package com.schoolhelper.model.dao.okhttp;

import android.os.Environment;

import com.schoolhelper.model.Model;
import com.schoolhelper.model.dao.jsonparse.LwlControllerJsonParser;
import com.schoolhelper.model.dao.jsonparse.LwlJsonParser;
import com.schoolhelper.model.entity.Order;
import com.schoolhelper.model.entity.User;
import com.schoolhelper.utils.constant.Api;
import com.schoolhelper.model.dao.jsonparse.EasyCodeJsonParser;
import com.schoolhelper.model.dao.jsonparse.JsonParser;
import com.schoolhelper.utils.constant.LwlApi;

import org.junit.Test;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import lombok.SneakyThrows;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

//import static com.schoolhelper.constant.DetailApi.GET_ORDER_WITH_IMG;

public class RefactorRequestUrlTest {
    static String baseUrl = Api.BASE_URL;
    Map<String, String> headers = new HashMap<>();

    @Test
    public void test1() {
//        JsonParser jsonParser = new YyzControllerJsonParser();
//
//        List<OrderImgVo> orderImgVoList = RefactorRequestUrl.postThenGetList(
//                baseUrl, GET_ORDER_WITH_IMG, null,
//                OrderImgVo.class, jsonParser);
//
//        System.out.println(orderImgVoList);
    }

    @Test
    public void test2(){
        System.out.println("查询所有user");
        JsonParser jsonParser = new EasyCodeJsonParser();

        List<User> users = RefactorRequestUrl.getThenGetList(
                baseUrl, Api.USER, headers, null,
                User.class, jsonParser);

        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void testPostOrder() {
        JsonParser jsonParser = new EasyCodeJsonParser();
        Order order = new Order(7L,
                1,
                "拿快递",
                "快递",
                "这是一个描述",
                5,
                "0",
                2,
                new Date(),
                new Date(),
                new Date());
        System.out.println(order.toString());
        boolean code = RefactorRequestUrl.justPost(baseUrl, Api.ORDER, headers, order, jsonParser);
        System.out.println(code);
    }

    @Test
    @SneakyThrows
    public void testGetHx() {
        headers.put("Authorization", "Bearer YWMtpfYvpLH_EeueFYMIjFDhEAAAAAAAAAAAAAAAAAAAAAGmyQvaKRRKj7E5qmQuirlJAgMAAAF5WTsLmABPGgBmDe3euNTM1fqhHu2fsVLO19qYOU5vzgAOAA9Ax5dUCQ");
        OkHttpClient client = OkHttpUtil.getInstance();
        Request request = new Request.Builder()
                .url(Api.BASE_HXURL + "/" + "users")
                .headers(Headers.of(headers))
                .get()
                .build();

        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                Response response = client.newCall(request).execute();
                return response.body().string();
            }
        };
        Future<String> response = Model.getInstance().getGlobalThreadPool().submit(callable);
        System.out.println(response.get());
    }

    @Test
    public void testPostSmms() {
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "mc1bMY0iL1hW9N478FWQ0AZ3B11TqBw9");
        LwlJsonParser jsonParser = new LwlControllerJsonParser();
        File image = new File("/storage/self/primary/PxEz/45060919.png");
        System.out.println(image.exists());
        String postImageResult = LwlRequestUrl.postSmmsThenGetUrl(LwlApi.BSER_SMMSURL, LwlApi.SMMS_UPLOAD, headers, image, jsonParser);
        System.out.println(postImageResult);
        System.out.println(LwlRequestUrl.getPostSmms(LwlApi.BSER_SMMSURL, LwlApi.SMMS_UPLOAD, headers, image));
    }

    @Test
    public void testGetLastOrder() {
        Map<String, String> headers = new HashMap<>();
        LwlJsonParser lwlJsonParser = new LwlControllerJsonParser();
        Order lastOrder = RefactorRequestUrl.postThenGetOne(Api.BASE_URL, LwlApi.GET_LAST_ORDER, headers, null, Order.class, lwlJsonParser);
    }

    @Test
    public void testPostHxUser() {
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Bearer YWMtpfYvpLH_EeueFYMIjFDhEAAAAAAAAAAAAAAAAAAAAAGmyQvaKRRKj7E5qmQuirlJAgMAAAF5WTsLmABPGgBmDe3euNTM1fqhHu2fsVLO19qYOU5vzgAOAA9Ax5dUCQ");
        LwlJsonParser lwlJsonParser = new LwlControllerJsonParser();
        Map<String, String> param = new HashMap<>();
        param.put("username", "456789");
        param.put("password", "123456");
//        boolean users = RefactorRequestUrl.justPost("https://a1.easemob.com/1101210504148104/sh", "users", headers, null, lwlJsonParser);
        String responseData = RefactorRequestUrl.getPostResponseJson("https://a1.easemob.com/1101210504148104/sh", "users", headers, param);
        System.out.println(responseData);
    }

    @Test
    public void testPostUser() {
        Map<String, String> headers = new HashMap<>();
//        headers.put("Authorization", "Bearer YWMtpfYvpLH_EeueFYMIjFDhEAAAAAAAAAAAAAAAAAAAAAGmyQvaKRRKj7E5qmQuirlJAgMAAAF5WTsLmABPGgBmDe3euNTM1fqhHu2fsVLO19qYOU5vzgAOAA9Ax5dUCQ");
        LwlJsonParser lwlJsonParser = new LwlControllerJsonParser();
        User user = new User(null, "1234567", "1234567", "0", "广外", "1234567", "1234567", "", 100);
//        boolean users = RefactorRequestUrl.justPost("https://a1.easemob.com/1101210504148104/sh", "users", headers, null, lwlJsonParser);
        String responseData = RefactorRequestUrl.getPostResponseJson(Api.BASE_URL, Api.USER, headers, user);
        System.out.println(responseData);
    }
}
